﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace DALL.Progect
{
  public class T_PUB_PROJECTMATERIAL
    {
        public T_PUB_PROJECTMATERIAL()
        { }
        #region  Method

        public DataSet GetPList(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT p.ID, \n");
            varname1.Append("       p.MATERIAL_ID, \n");
            varname1.Append("       p.NUM, \n");
            varname1.Append("       p.REMARK, \n");
            varname1.Append("       m.MATERIAL_NAME \n");
            varname1.Append("FROM   T_PUB_PROJECTMATERIAL p, \n");
            varname1.Append("       T_PUB_MATERIAL m \n");
            varname1.Append("WHERE  p.MATERIAL_ID = m.MATERIAL_ID ");
            if (strWhere.Trim() != "")
            {
                varname1.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.Progect.T_PUB_PROJECTMATERIAL model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_PROJECTMATERIAL(");
            strSql.Append("PROJECT_ID,MATERIAL_ID,NUM,FLAG,REMARK)");

            strSql.Append(" values (");
            strSql.Append("@PROJECT_ID,@MATERIAL_ID,@NUM,@FLAG,@REMARK)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECT_ID", DbType.String, model.PROJECT_ID);
            db.AddInParameter(dbCommand, "MATERIAL_ID", DbType.Int32, model.MATERIAL_ID);
            db.AddInParameter(dbCommand, "NUM", DbType.Int32, model.NUM);
            db.AddInParameter(dbCommand, "FLAG", DbType.Boolean, model.FLAG);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Progect.T_PUB_PROJECTMATERIAL model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_PROJECTMATERIAL set ");
            strSql.Append("PROJECT_ID=@PROJECT_ID,");
            strSql.Append("MATERIAL_ID=@MATERIAL_ID,");
            strSql.Append("NUM=@NUM,");
            strSql.Append("FLAG=@FLAG,");
            strSql.Append("REMARK=@REMARK");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Decimal, model.ID);
            db.AddInParameter(dbCommand, "PROJECT_ID", DbType.String, model.PROJECT_ID);
            db.AddInParameter(dbCommand, "MATERIAL_ID", DbType.Int32, model.MATERIAL_ID);
            db.AddInParameter(dbCommand, "NUM", DbType.Int32, model.NUM);
            db.AddInParameter(dbCommand, "FLAG", DbType.Boolean, model.FLAG);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_PROJECTMATERIAL ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Decimal, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Progect.T_PUB_PROJECTMATERIAL GetModel(decimal ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PROJECT_ID,MATERIAL_ID,NUM,FLAG,REMARK from T_PUB_PROJECTMATERIAL ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Decimal, ID);
            Model.Progect.T_PUB_PROJECTMATERIAL model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PROJECT_ID,MATERIAL_ID,NUM,FLAG,REMARK ");
            strSql.Append(" FROM T_PUB_PROJECTMATERIAL ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Progect.T_PUB_PROJECTMATERIAL> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PROJECT_ID,MATERIAL_ID,NUM,FLAG,REMARK ");
            strSql.Append(" FROM T_PUB_PROJECTMATERIAL ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.Progect.T_PUB_PROJECTMATERIAL> list = new List<Model.Progect.T_PUB_PROJECTMATERIAL>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Progect.T_PUB_PROJECTMATERIAL ReaderBind(IDataReader dataReader)
        {
            Model.Progect.T_PUB_PROJECTMATERIAL model = new Model.Progect.T_PUB_PROJECTMATERIAL();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ID =Convert.ToDecimal(ojb);
            }
            model.PROJECT_ID = dataReader["PROJECT_ID"].ToString();
            ojb = dataReader["MATERIAL_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MATERIAL_ID = (int)ojb;
            }
            ojb = dataReader["NUM"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.NUM = (int)ojb;
            }
            ojb = dataReader["FLAG"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FLAG = (bool)ojb;
            }
            model.REMARK = dataReader["REMARK"].ToString();
            return model;
        }

        #endregion  Method
    }
}
